package com.edu.bean;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @Author:mx
 * @Date:2025/11/7-9:28:57
 */
public class MyIOC2 {

    //提供一个东西 来装 其他的被管理的对象
    private Map<Object,Object> box;

    private Properties pro ;

    private Properties objectProperty;


    public MyIOC2(String path){
        box = new HashMap<>();
        objectProperty = new Properties();
        readPath(path);
    }

    public Object getBean(String beanId){
        return box.get(beanId);
    }

    public Object getBean(Class clazz){
        Set<Object> keySet = box.keySet();
        for (Object key : keySet) {
            Object value = box.get(key);
            if(value.getClass() == clazz){
                return value;
            }
        }
        return null;
    }

    //只负责读取 配置文件
    public void readPath(String path){
        pro = new Properties();
        InputStream inputStream = MyIOC2.class.getClassLoader().getResourceAsStream(path);
        try {
            pro.load(inputStream);
            init();
        } catch (IOException e) {
            System.out.println("配置文件不存在");
            e.printStackTrace();
        }
    }
    //根据配置文件里的数据，创建对象，并且把对象放入到box
    public void init(){
        Set<String> beanIds = pro.stringPropertyNames();
        //创建对象
        for (String id : beanIds) {
            String className = pro.getProperty(id);
            try {
                //判断是 bean 还是 bean的属性
                if(id.contains(".")){
                    objectProperty.put(id,pro.getProperty(id));
                    continue;
                }
                Class clazz = Class.forName(className);
                Object o = clazz.newInstance();
                box.put(id,o);
            } catch (ClassNotFoundException e) {
                System.out.println("找不到指定的类型");
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                System.out.println("没有公开的无参构造方法");
                e.printStackTrace();
            }
        }
        setBeanProperty();
    }

    public void setBeanProperty(){
        Set<String> keys = objectProperty.stringPropertyNames();
        for (String key : keys) {
            int index = key.indexOf(".");
            String beanId = key.substring(0, index);
            Object o = box.get(beanId);
            if(o == null){
                System.out.println(beanId+"的对象不存在");
                continue;
            }
            String fieldName = key.substring(index+1);
            Class clazz = o.getClass();
            try {
                Field field = clazz.getDeclaredField(fieldName);
                String value = objectProperty.getProperty(key);
                field.setAccessible(true);
                Class targetClass = field.getType();
                Object finalValue = typeTrans(value,targetClass);
                field.set(o,finalValue);
            } catch (NoSuchFieldException e) {
                System.out.println(clazz.getName()+"不包含这个属性"+fieldName);
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public Object typeTrans(String str,Class targetClass){
        if(targetClass.getSimpleName().equals("int")){
            int i = Integer.parseInt(str);
            return i;
        }
        return str;
    }
}
